package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.constant.Validate;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.pojo.Users;
import com.luchao.back.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "前台用户管理")
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @ApiOperation("前台用户登录")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "uaccount", value = "前台用户账号", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = ""),
            @ApiImplicitParam(name = "upwd", value = "前台用户密码", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/login")
    public ResponseResult login(String uaccount, String upwd) {
        Users data = usersService.login(uaccount, upwd);
        if (data != null) {
            if (data.isUdelete()) {
                return ResponseResult.success(ResponseResult.FAILED, "账号已被锁定,请联系客服", null);
            } else {
                return ResponseResult.success(ResponseResult.SUCCESS, "登录成功", data);
            }
        } else {
            return ResponseResult.success(ResponseResult.FAILED, "账号与密码匹配错误,登录失败", null);
        }

    }

    @ApiOperation("校验用户账号唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "uaccount", value = "前台用户账号", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkName")
    public ResponseResult checkName(String uaccount) {
        return usersService.checkName(uaccount) > 0 ? ResponseResult.success(ResponseResult.FAILED, "用户名已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "用户名不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("前台用户注册")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "user", value = "前台用户对象信息", required = true, paramType = "formData", dataType = "Users", dataTypeClass = Users.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/register")
    public ResponseResult register(@RequestBody Users user) {
        return usersService.register(user) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "注册成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "注册失败", Execute.ERROR);
    }

    @ApiOperation("修改前台用户信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "users", value = "前台用户对象信息", required = true, paramType = "formData", dataType = "Users", dataTypeClass = Users.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/editUsers")
    public ResponseResult editUsers(@RequestBody Users users) {
        return usersService.editUsers(users) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "用户已更新", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "用户更新失败", Execute.ERROR);
    }

    @ApiOperation("分页获取用户列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "当前页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getUsersByPage")
    public ResponseResult getUsersByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        long total = usersService.getUsersCount();
        long totalPages = total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1;
        pageNum = total <= pageSize ? 1 : pageNum;
        PageResult<Users> pageResult = new PageResult<Users>();
        pageResult.setPageNum(pageNum);
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(usersService.getUsersByPage(pageNum, pageSize));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("根据ID获取用户信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "uid", value = "前台用户ID", required = true, paramType = "query", dataType = "int", dataTypeClass = int.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getUsersById")
    public ResponseResult getUsersById(int uid) {
        return ResponseResult.success(usersService.getUsersById(uid));
    }

    @ApiOperation("批量禁用/解禁前台用户")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "list", value = "前台用户信息列表", required = true, paramType = "formData", dataType = "List<Users>", dataTypeClass = List.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/batchChangeState")
    public ResponseResult batchChangeState(@RequestBody List<Users> list) {
        return usersService.batchChangeState(list) ? ResponseResult.success(ResponseResult.SUCCESS, "操作成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "操作失败", Execute.ERROR);
    }
}
